////////////////////////////////////////////////////////////////
// filename: sound.cpp
// description: source file for our sound engine
// autor: Jay Tomten (jaytomten@gmail.com)
// notes:

// namespace
using namespace std;

// defines

// includes
#include "sound.h"
#include <iostream>

// globals

////////////////////////////////////////////////////////////////
// constructor
Sound::Sound() {

    // use Init();

}

////////////////////////////////////////////////////////////////
// destructor
Sound::~Sound() {

    Mix_FreeMusic(music);
    Mix_FreeChunk(sfxDie1);
    Mix_FreeChunk(sfxDie2);
    Mix_FreeChunk(sfxGun1);
    Mix_FreeChunk(sfxGun2);
    Mix_FreeChunk(sfxGun3);
    Mix_FreeChunk(sfxBlip);
    Mix_FreeChunk(sfxBleep);
    Mix_FreeChunk(sfxClick);
    Mix_FreeChunk(sfxExplode);
    Mix_CloseAudio();

}

////////////////////////////////////////////////////////////////
// Init() - load up our audio system
bool Sound::Init(int musicVolume, int soundFXVolume) {

    rate = 44100; // audio rate
    format = AUDIO_S16SYS; // 16 bit
    channels = 2;
    buffer = 4096;
    if(Mix_OpenAudio(rate, format, channels, buffer) == -1) exit(1);
    // keep sfx in memory
    sfxDie1 =  Mix_LoadWAV("../res/sfx/die1.ogg");
    sfxDie2 =  Mix_LoadWAV("../res/sfx/die2.ogg");
    sfxGun1 =  Mix_LoadWAV("../res/sfx/gun1.ogg");
    sfxGun2 =  Mix_LoadWAV("../res/sfx/gun2.ogg");
    sfxGun3 =  Mix_LoadWAV("../res/sfx/gun3.ogg");
    sfxBlip =  Mix_LoadWAV("../res/sfx/blip.ogg");
    sfxBleep = Mix_LoadWAV("../res/sfx/bleep.ogg");
    sfxClick = Mix_LoadWAV("../res/sfx/click.ogg");
    sfxExplode = Mix_LoadWAV("../res/sfx/explode.ogg");
    Mix_VolumeMusic(musicVolume);
    Mix_Volume(-1, soundFXVolume);
    cout << "Mixer loaded!\n";
    return true;

}

////////////////////////////////////////////////////////////////
// PlayMusic() - play a music track
bool Sound::PlayMusic(int s) {

    Mix_HaltMusic(); // stop the tunes
    Mix_FreeMusic(music); // free previous track
    // load new track
    if(s == MUSIC_1)     music = Mix_LoadMUS("../res/music/level1.ogg");
    if(s == MUSIC_2)     music = Mix_LoadMUS("../res/music/level2.ogg");
    if(s == MUSIC_3)     music = Mix_LoadMUS("../res/music/level3.ogg");
    if(s == MUSIC_4)     music = Mix_LoadMUS("../res/music/level4.ogg");
    if(s == MUSIC_5)     music = Mix_LoadMUS("../res/music/level5.ogg");
    if(s == MUSIC_6)     music = Mix_LoadMUS("../res/music/level6.ogg");
    if(s == MUSIC_7)     music = Mix_LoadMUS("../res/music/level7.ogg");
    if(s == MUSIC_8)     music = Mix_LoadMUS("../res/music/level8.ogg");
    if(s == MUSIC_MENU)     music = Mix_LoadMUS("../res/music/levelmenu.ogg");
    if(s == MUSIC_STORY)     music = Mix_LoadMUS("../res/music/levelstory.ogg");
    cout << "\nAttempting to play music...\n";
    Mix_PlayMusic(music, -1);
    return true;

}

////////////////////////////////////////////////////////////////
// StopMusic() - pause playing for a bit
void Sound::StopMusic() {

    Mix_HaltMusic();

}

////////////////////////////////////////////////////////////////
// PlaySFX() - play a sound effect
bool Sound::PlaySFX(int fx) {

    // cout << "Attempting to play sfx " << fx << "...\n";

    if(fx == SFX_DIE1) Mix_PlayChannel(-1, sfxDie1, 0);
    if(fx == SFX_DIE2) Mix_PlayChannel(-1, sfxDie2, 0);
    if(fx == SFX_GUN1) Mix_PlayChannel(-1, sfxGun1, 0);
    if(fx == SFX_GUN2) Mix_PlayChannel(-1, sfxGun2, 0);
    if(fx == SFX_GUN3) Mix_PlayChannel(-1, sfxGun3, 0);
    if(fx == SFX_BLIP) Mix_PlayChannel(-1, sfxBlip, 0);
    if(fx == SFX_BLEEP) Mix_PlayChannel(-1, sfxBleep, 0);
    if(fx == SFX_CLICK) Mix_PlayChannel(-1, sfxClick, 0);
    if(fx == SFX_EXPLODE) Mix_PlayChannel(-1, sfxExplode, 0);
    return true;

}
